package com.peng.leetcode.tree;

/**
 * GetMinimumDifference
 * 530. 二叉搜索树的最小绝对差
 * https://leetcode.cn/problems/minimum-absolute-difference-in-bst/
 * <p>
 * Created on 2022/12/6
 *
 * @author lupeng
 */
public class GetMinimumDifference {
    public int getMinimumDifference(TreeNode root) {
        if (root == null) {
            return 0;
        }
        dfs(root);
        return ans;
    }
    Integer pre;
    int ans = Integer.MAX_VALUE;
    void dfs(TreeNode n) {
        if (n == null) {
            return;
        }
        dfs(n.left);
        if (pre != null) {
            ans = Math.min(ans, Math.abs(pre - n.val));
        }
        pre = n.val;
        dfs(n.right);
    }
}
